package org.wanda.pv.controller.business;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.business.Mark;
import org.wanda.pv.model.business.Topic;
import org.wanda.pv.model.hy.HyUserInfo;
import org.wanda.pv.model.user.SysUser;
import org.wanda.pv.service.business.TopicService;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxy
 * @date 2018/11/24 10:47
 */
@RestController
@RequestMapping("/business/topic")
public class TopicController {
    private final Log log = LogFactory.getLog(this.getClass());

    @Autowired
    private TopicService topicService;

    @Autowired
    private Dao dao;

    @RequestMapping("/topicList")
    public Object topicList(Long markId){
        List<Topic> topicList = new ArrayList<>();
        Sql sql = Sqls.create("select topic_id from wd_re_mark_topic where mark_id = @markId");
        sql.setParam("markId",markId);
        sql.setCallback(new SqlCallback() {
            List<Long> ids = new ArrayList<>();
            @Override
            public Object invoke(Connection connection, ResultSet rs, Sql sql) throws SQLException {
                while (rs.next()){
                    Long topic_id = rs.getLong("topic_id");
                    ids.add(topic_id);
                }
                return ids;
            }
        });
        dao.execute(sql);
        List<Long> topicIds = sql.getList(Long.class);
        if(topicIds!=null){
            for (Long topicId : topicIds) {
                Topic topic = dao.fetch(Topic.class, Cnd.where("id", "=", topicId).and("delFlag","=",0));
                dao.fetchLinks(topic,null);
                HyUserInfo hyUserInfo=dao.fetch(HyUserInfo.class,topic.getHyUserInfoId());
                topic.setHyUserInfo(hyUserInfo);
                topicList.add(topic);
            }
        }
        return Result.success("success",topicList);
    }


    @RequestMapping("/addRevertByMark")
    public Object addRevertByMark(HttpServletRequest request,Long markId, String content, Long pointId){
        try {
            HyUserInfo sysUser= (HyUserInfo) request.getSession().getAttribute("userInfo");
            if (pointId == null){
                Mark mark =dao.fetch(Mark.class,markId);
                pointId = mark.getPointId();
            }
            topicService.addRevertByMark(markId,content,sysUser.getId(),pointId);
            return Result.success("success","操作成功!");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return Result.error("fail","服务器内部错误!");        }

    }

}
